﻿$(document).ready(Page_Load);


function Page_Load(e) {
    LoadGrid();
    $('.button').button();

    $('[name="btnClearLog"]').click(function () {
        ClearLog();
    });
}

function LoadGrid() {

    $("#list2").jqGrid(
        {
            prmNames: {
                search: "isSearch",
                nd: null,
                rows: "numRows",
                page: "page",
                sort: "sortField",
                order: "sortOrder"
            },
            // add by default to avoid webmethod parameter conflicts
            postData: { searchString: '', searchField: '', searchOper: '' },
            // setup ajax call to webmethod
            datatype: function (postdata) {
                $(".loading").show(); // make sure we can see loader text
                $.ajax({
                    url: 'Logs.aspx/ReadLog',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(postdata),
                    dataType: "json",
                    success: function (data, st) {
                        if (st == "success") {
                            var grid = $("#list2")[0];
                            grid.addJSONData(data.d);
                        }
                    },
                    error: function () {
                        Log(XMLHttpRequest, "Logs.aspx.js LoadGrid", 100);
                        alert("Error with AJAX callback");
                    }
                });
            },
            // this is what jqGrid is looking for in json callback
            jsonReader: {
                root: "rows",
                page: "page",
                total: "totalpages",
                records: "totalrecords",
                //cell: "cell",
                id: "id", //index of the column with the PK in it 
                userdata: "userdata",
                repeatitems: false
            },
            colNames: ['Id', 'EventId', 'Priority', 'Severity', 'Timestamp', 'Machine Name', 'Category', 'Message'],
            colModel: [
            { name: 'LogId', index: 'LogId', width: 55, search: false },
            { name: 'EventId', index: 'EventId', width: 55 },
            { name: 'Priority', index: 'Priority', width: 55 },
            { name: 'Severity', index: 'Severity', width: 75 },
            { name: 'Timestamp', index: 'Timestamp', width: 125 },
            { name: 'MachineName', index: 'MachineName', width: 100 },
            { name: 'CategoryName', index: 'CategoryName', width: 150 },
            { name: 'Message', index: 'Message', width: 300 }
            ],
            rowNum: 50,
            pager: '#pager2',
            sortname: 'LogId',
            viewrecords: true,
            sortorder: "desc",
            caption: "Log",
            height: 250,
            width: "100%",
            editurl: 'somepage.aspx',
            gridComplete: function () {
                $(".loading").hide();
            }
        });

        $("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false, beforeRefresh: function () { GetRoles(); }, closeOnEscape: true, closeAfterSearch: true })

        var roledata = $("#list2").jqGrid('getRowData');
    }

    function ClearLog() {
        var $dialog = $('<div></div>')
		.html('Are you sure you want to clear the log file?')
		.dialog({
		    autoOpen: false,
		    resizable: false,
		    height: 200,
		    width: 400,
		    modal: true,
		    buttons: {
		        'Yes': function () {
		            ClearConfirmed();
		            $(this).dialog('close');
		        },
		        'No': function () {
		            $(this).dialog('close');
		        }
		    },
		    title: 'Confirm Delete'
		});
        $dialog.dialog('open');
    }

    function ClearConfirmed() {
        $.ajax(
            {
                type: "POST",
                async: true,
                url: "Logs.aspx/ClearLog",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: ClearLog_Success,
                error: ClearLog_Error
            });
        }

        function ClearLog_Success(data, textStatus, XMLHttpRequest) {
            LoadGrid();
        }

        function ClearLog_Error(XMLHttpRequest, textStatus, errorThrown) {
            Log(XMLHttpRequest, "ClearLog", 100);
            alert("An error occurred while clearing the log. Error: " + XMLHttpRequest);

        }